package com.example.mobile2021606159yhf0413;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnbutton1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //关闭按钮
        btnbutton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //说明对话框对象
                AlertDialog dialog;
                //定义对话框创建对象
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                //对话框标题
                builder.setTitle("中国银行提醒您");
                //对话框内容
                builder.setMessage("是否确定退出");
                //添加退出取消按钮
                builder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();//关闭对话框
                        MainActivity.this.finish();//关闭Activity
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        MainActivity.this.finish();
                    }
                });
                //创建AlertDialog对象
                dialog=builder.create();
                //显示对话框
                dialog.show();

            }
        });
    }
    public void initView(){
        btnbutton1 = (Button) findViewById(R.id.button);
    }
}